package com.segersten.astro

import java.util.logging.Logger
import javax.persistence.Entity;

class AdminController {

	Logger LOG = Logger.getLogger(AdminController.class.getCanonicalName())
	
    def index = { render(text:"OK adminSentences adminSigns")}
	
	def adminSentences = { 
		LOG.info("adminSentences")
		List sentences = Sentence.findAll("SELECT FROM Sentence AS s ORDER BY s.language")
		[sentences:sentences]
	}
	
	def addSentence = {
		LOG.info("addSentence")
		def sentences = split(params.sentence, new ArrayList<String>())
		sentences.each { sentence -> 
			if (sentence.length()>0) {
				Sentence.withTransaction { status ->
					Sentence s = new Sentence()
					s.sentence = sentence
					s.language = params.language
					s.metaData = params.metaData
					s.months = params.months
					s.save()
					LOG.info("Added $s")
				}
			}
		}	
		redirect(action:adminSentences)
	}
	
	def editSentence = {
		LOG.info("editSentence")
		def sentence = Sentence.get(params.id)
		[sentence:sentence]
	}
	
	def updateSentence = {
		LOG.info("updateSentence")
		def sentence = Sentence.get(params.id)
		sentence.sentence = params.sentence
		sentence.language = params.language
		sentence.metaData = params.metaData
		sentence.months = params.months
		sentence.save()
		LOG.info("Updated $sentence")
		redirect(action:adminSentences)
	}
	
	def deleteSentence = {
		LOG.info("deleteSentence")
		def sentence = Sentence.get(params.id)
		sentence.delete()
		LOG.info("Deleted $sentence <$params.id>")
		redirect(action:adminSentences)
	}
	
	def adminSigns = { 
		LOG.info("adminSigns")
		List signs = Sentence.findAll("SELECT FROM Sign AS s ORDER BY s.language")
		[signs:signs]
	}
	
	def addSign = {
		LOG.info("addSign")
		def sign = new Sign()
		sign.descr2 = params.description
		sign.language = params.language
		sign.signId = Integer.parseInt(params.signId)
		sign.period = params.period
		sign.name = params.name
		sign.save()
		redirect(action:adminSigns)
	}
	
	def editSign = {
		LOG.info("editSign")
		def sign = Sign.get(params.id)
		[sign:sign]
	}
	
	def updateSign = {
		LOG.info("updateSign")
		def sign = Sign.get(params.id)
		sign.descr2 = params.description
		sign.language = params.language
		sign.signId = Integer.parseInt(params.signId)
		sign.period = params.period
		sign.name = params.name
		sign.save()
		LOG.info("Updated $sign")
		redirect(action:adminSigns)
	}
	
	def deleteSign = {
		LOG.info("deleteSign")
		def sign = Sign.get(params.id)
		sign.delete()
		LOG.info("Deleted $sign <$params.id>")
		redirect(action:adminSigns)
	}
	
	def test = {
		def rs = split(params.text, new ArrayList<String>())
		render(text:rs.toString())
	}
	
	private List<String> split(String text, List<String> result) {
		int index1 = text.indexOf(".");
		//LOG.info("1 ${index1}}")
		int index2 = text.indexOf("!");
		//LOG.info("2 ${index1}}")
		int index3 = text.indexOf("?");
		//LOG.info("3 ${index1}}")
		int textindex = index1==-1 ? Integer.MAX_VALUE : index1;
		textindex = index2==-1 ? textindex : Math.min(textindex, index2);
		textindex = index3==-1 ? textindex : Math.min(textindex, index3);
		
		if (textindex == Integer.MAX_VALUE) {
			if (text.trim().length()>0) {
				result.add(text.trim() + ".")
			}
			return result
		}
		textindex += 1 // include the .!?
		def head = text.substring(0, textindex)
		//LOG.info("Head[$textindex] '$head'")
		result.add(head.trim())
		
		def tail = text.substring(textindex)
		//LOG.info("Tail '$tail'")
		return split(tail,result)
	}
	
}
